package cn.tedu.charging.user.service;

import cn.tedu.charging.common.constant.AppInfoConst;
import cn.tedu.charging.common.pojo.param.ChargeParam;
import cn.tedu.charging.common.pojo.param.VehicleBindParam;
import cn.tedu.charging.common.pojo.po.ChargingUserInfoPO;
import cn.tedu.charging.common.pojo.vo.BalanceVO;
import cn.tedu.charging.common.pojo.vo.VehicleVO;
import cn.tedu.charging.user.mapper.UserMapper;
import cn.tedu.charging.user.pojo.WxLoginDTO;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.Date;

@Service
@Slf4j
public class UserServiceImpl implements UserService{
    @Autowired
    private UserMapper userMapper;

    public UserServiceImpl(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    public Integer wxLogin(String nickName, String code) {
        log.debug("微信登录，入参昵称:{},临时授权码:{}",nickName,code);
        //1.获取open开放id
        String openId=getWxOpenId(code);
        if (openId==null){
            //2.获取失败 没拿到 登录结束
            //本次登录结束
            return null;
        }
        //3.拿到openId 检查 系统是否存在绑定小程序用户
        //select * from user where wx_open_id=#{openId}
        QueryWrapper userWrapper=new QueryWrapper();
        userWrapper.eq("wx_open_id",openId);
        ChargingUserInfoPO userPO = userMapper.selectOne(userWrapper);
        if (userPO==null){
            log.debug("当前用户:{},第一次登录,注册车主数据",openId);
            userPO=new ChargingUserInfoPO();
            userPO.setNickName(nickName);
            //绑定openId
            userPO.setWxOpenId(openId);
            userPO.setCreateTime(new Date());
            userPO.setUpdateTime(new Date());
            userMapper.insert(userPO);

        }
        return userPO.getId();
        /*log.debug("本地登录注册,入参:{}",nickName);
        //1.查询该昵称|手机号 是否在表格存在用户车主数据
        //select * from user where nick_name = #{nickName}
        QueryWrapper userWrapper = new QueryWrapper();
        userWrapper.eq("nick_name",nickName);
        ChargingUserInfoPO userPO = userMapper.selectOne(userWrapper);
        //2.判断是否存在
        if(userPO == null){
            log.debug("用户不存在,开始注册");
            userPO = new ChargingUserInfoPO();
            userPO.setNickName(nickName);
            userPO.setCreateTime(new Date());
            userPO.setUpdateTime(new Date());
            userMapper.insert(userPO);
            //mybatis框架 会对自增id做回填
        }
        //3.返回用户id
        return userPO.getId();
         */
    }

    private String getWxOpenId(String code) {
        RestTemplate restTemplate = new RestTemplate();
        //getForObject
        String url="https://api.weixin.qq.com/sns/jscode2session?" +
                "appid={1}&secret={2}&" +
                "js_code={3}&grant_type=authorization_code";
        //发送请求
        String json = restTemplate.getForObject
                (url, String.class,
                        AppInfoConst.APP_ID,
                        AppInfoConst.APP_SECRET,code);
        //将json反向解析转化成对象 很多api方法都可以实现json和对象之间互转
        //json工具库 jackson fastjson fastjson2
        WxLoginDTO wxLoginDTO = JSON.parseObject(json, WxLoginDTO.class);
        return wxLoginDTO.getOpenid();
    }

    @Override
    public BalanceVO myBalance(Integer userId) {
        return null;
    }

    @Override
    public void charge(ChargeParam param) {

    }

    @Override
    public VehicleVO bindedVehicle(Integer userId) {
        return null;
    }

    @Override
    public void bindVehicle(VehicleBindParam param) {

    }
}
